var map;
var infowindow;
var marker;

var searchfield_datasource = function(query,callback){
    $("#searchbox").removeClass("error");
    $("#searchbox").removeClass("warning");
    if(checkQuery(query) == 2){
    //Koordinatensuche
    }else if(checkQuery(query) == 1){
        //Gemischte Suche
        $.ajax({
            dataType: "json",
            url: "/WeatherAppFrontend/webresources/location/search/"+encodeURI(escape(query)),
            success: function(data,status,xhr){
                if(data != null){
                    var resultArray = [];
                    if(data.wetterComLocation.length > 0){
                        for(var i = 0; i < data.wetterComLocation.length; i++){
                            resultArray.push(getReadableString(data.wetterComLocation[i]));
                        }
                    }else{
                        resultArray.push(getReadableString(data.wetterComLocation));
                    }
                    callback(resultArray);
                }
            }
        });
    }else{
        $("#searchbox").addClass("warning");
    }
}

var searchbtnClickhandler = function(e){    
    var query = $("#searchfield").val();
        
    if(checkQuery(query) != 2){
        query = query.replace(/\(/g,"");
        query = query.replace(/\)/g,"");
        query = query.replace(/,/g,"");
        
        if(checkQuery(query) == 0){
            $("#searchbox").addClass("error");
            return;
        }
    }
    
    $("#searchbtn").button('loading');
    $("#searchfield").attr('disabled', 'disabled');

    $.ajax({
        dataType: "json",
        url: "/WeatherAppFrontend/webresources/weather/search/"+query,
        success: function(data,status,xhr){
            if(xhr.status == 202){
                pollWeather(data.ressourceId);
            }else{
                alert("Ein ungeahnter Fehler ist aufgetreten: " + xhr.statusText);
                $("#searchbtn").button('reset');
                $("#searchfield").removeAttr('disabled');
            }
        },
        error: function(xhr,status,err){
            if(xhr.status == 400){//
                alert("Query does not match to given format");
            }else if(xhr.status == 412){
                alert("Der Ort konnte nicht gefunden werden");
            }else{
                alert("Ein ungeahnter Fehler ist aufgetreten: " + xhr.statusText);
            }
            
            $("#searchbtn").button('reset');
            $("#searchfield").removeAttr('disabled');
        }
    });
}

function pollWeather(ressourceId){
    $.ajax({
        dataType: "json",
        url: "/WeatherAppFrontend/webresources/weather/"+ressourceId,
        success: function(data,status,xhr){
            if(xhr.status == 304){
                var doAgain = function(){
                    pollWeather(ressourceId)
                }
                setTimeout(doAgain,500);
            }else if(xhr.status == 200){
            
                var tmpLocation = data.location;
                map.fitBounds(new google.maps.LatLngBounds(new google.maps.LatLng(tmpLocation.bounds.southwest.lat,tmpLocation.bounds.southwest.lon),new google.maps.LatLng(tmpLocation.bounds.northeast.lat,tmpLocation.bounds.northeast.lon)));

                var infoContent = "<div id='weatherInfoWindow'>";
                infoContent +=  "<div class='page-header'>"
                infoContent += "<h3>" +  data.location.name + " (" + data.location.zip + ")<br/><small>" + data.location.state + ", " + data.location.county + "</small></h3></div>"
                var tmpDate = new Date(data.forecast.localTimestamp * 1000);
                infoContent += "<strong>Heute am " + getDayName(tmpDate.getUTCDay()) + ", den " + tmpDate.getUTCDate() + "." + (tmpDate.getUTCMonth() + 1) + "." + tmpDate.getUTCFullYear() + ":</strong>"
                
                infoContent += "<br/><br/><img src='/WeatherAppFrontend/resources/img/d_" + data.forecast.weatherCode.charAt(0) + "_b.png'/>";
                infoContent += "" + data.forecast.weatherTxt + " bei " + data.forecast.minTemp + " °C bis " + data.forecast.maxTemp + " °C";
                
                infoContent += "<br/><img src='/WeatherAppFrontend/resources/img/" + data.forecast.windDirectionText + ".png'/>";
                infoContent += "Windrichtung " + data.forecast.windDirection + "° bei " + data.forecast.windSpeed + " km/h";
                
                infoContent += "<br/><small>Details bei <a href='http://www.wetter.com/" + data.detailUrl + "' target='_BLANK'>Wetter.com</a></small>"
                infoContent += "</div>"
                
                if(infowindow != null){
                    infowindow.close();
                }
                
                infowindow = new google.maps.InfoWindow({
                    content: infoContent,
                    maxWidth : 800,
                    disableAutoPan : true
                });
                
                if(marker != null){
                    marker.setMap(null);
                }

                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(tmpLocation.center.lat,tmpLocation.center.lon),
                    map: map,
                    title: tmpLocation.name
                });

                google.maps.event.addListener(marker, 'click', function() {
                    infowindow.open(map,marker);
                });

                infowindow.open(map,marker);
                mapRecenter(new google.maps.LatLng(tmpLocation.center.lat,tmpLocation.center.lon),0,-250);

                $("#searchbtn").button('reset');
                $("#searchfield").removeAttr('disabled');
           }else{
               alert("Fehler: " + xhr.statusText);
               $("#searchbtn").button('reset');
               $("#searchfield").removeAttr('disabled');
           }
        },
        error: function(xhr,status,err){
            alert(xhr.responseText);
            $("#searchbtn").button('reset');
            $("#searchfield").removeAttr('disabled');
        }
    });
}

function mapRecenter(latlng,offsetx,offsety) {
    var point1 = map.getProjection().fromLatLngToPoint(
        (latlng instanceof google.maps.LatLng) ? latlng : map.getCenter()
    );
    var point2 = new google.maps.Point(
        ( (typeof(offsetx) == 'number' ? offsetx : 0) / Math.pow(2, map.getZoom()) ) || 0,
        ( (typeof(offsety) == 'number' ? offsety : 0) / Math.pow(2, map.getZoom()) ) || 0
    );  
    map.setCenter(map.getProjection().fromPointToLatLng(new google.maps.Point(
        point1.x - point2.x,
        point1.y + point2.y
    )));
}

function getDayName(day){
    switch(day){
        case 0: return "Sonntag";
        case 1: return "Montag";
        case 2: return "Dienstag";
        case 3: return "Mittwoch";
        case 4: return "Donnerstag";
        case 5: return "Freitag";
        case 6: return "Samstag";
    }
}

function checkQuery(query){
    if(query.search(/^-{0,1}\d{1,2}\.\d+,-{0,1}\d{1,2}\.\d+$/) == 0){//Match Koordinaten
        return 2;
    }else if(query.search(/^[-A-Za-z0-9ßäÄüÜöÖ ]{2,}$/i) == 0){//Match Ort
        return 1;
    }else{
        return 0;
    }
}

function getReadableString(wetterComLocation){
    var tmpLine = wetterComLocation.name;
    tmpLine += wetterComLocation.zip != "" ? (" (" + wetterComLocation.zip + "), ") : ", ";
    tmpLine += wetterComLocation.district + (wetterComLocation.district != "" ? ", " : "");
    tmpLine += wetterComLocation.county + (wetterComLocation.county != "" ? ", " : "");
    tmpLine += wetterComLocation.state + (wetterComLocation.state != "" ? ", " : "");
    tmpLine += wetterComLocation.country + (wetterComLocation.country != "" ? " " : "");
    return tmpLine;
}


function initialize() {
    var mapOptions = {
        zoom: 3,
        center: new google.maps.LatLng(20.13847,-21.09375),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true,
        disableDoubleClickZoom : true,
        keyboardShortcuts : false
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
        mapOptions);

    $('#searchfield').attr("autocomplete","off");
    $('#searchfield').typeahead({
        source: searchfield_datasource,
        minLength : 1,
        items : 12,
        matcher : function(){
            return true;
        }
    });

    $('#searchbtn').click(searchbtnClickhandler);
}

google.maps.event.addDomListener(window, 'load', initialize);
